package com.tale.competition.service.impl;

import com.rabbitmq.client.Channel;
import com.tale.competition.common.config.RabbitConfig;
import com.tale.competition.common.constant.RabbitConstant;
import com.tale.competition.common.util.MailUtil;
import com.tale.competition.common.vo.MailBean;
import com.tale.competition.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * author: 蔡奇峰
 * date: 2020/4/29 23:19
 **/
@Service
public class MailServiceImpl implements MailService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Override
    public void send(MailBean mailBean) {

        rabbitTemplate.convertAndSend(RabbitConstant.EMAIL_EXCHANGE, RabbitConstant.EMAIL_ROUTING_KEY);
    }

    @Override
    public void sendTopic(Object msg){
        logger.info("topic 发送的消息:" + msg);

        amqpTemplate.convertAndSend(RabbitConstant.EMAIL_EXCHANGE, RabbitConstant.EMAIL_ROUTING_KEY, msg);
//        rabbitTemplate.convertAndSend(RabbitConstant.EMAIL_EXCHANGE, RabbitConstant.EMAIL_ROUTING_KEY, msg);
    }



    @Override
    public void sendMail(String msg) {



        send(msg, RabbitConstant.EMAIL_EXCHANGE, RabbitConstant.EMAIL_ROUTING_KEY);
    }

    private void send(String msg, String exchange, String routingKey) {
        String msgId = UUID.randomUUID().toString().replaceAll("-", "");


        MessageProperties properties = new MessageProperties();
        properties.setHeader("headers", "value");
        properties.setHeader("headers2", "value2");
        Message message = new Message(msg.getBytes(), properties);

        CorrelationData correlationData = new CorrelationData(msgId);

        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
        logger.info("rabbitmq消息已经发送到交换机, 等待交换机接受..." + msg);
    }

    @RabbitListener(queues = RabbitConstant.EMAIL_QUEUE)
    private void  consume(Message message, Channel channel){

        //将消息转化为对象
        String str = new String(message.getBody());
//        Mail mail = JsonUtil.strToObj(str, Mail.class);
//        log.info("收到消息: {}", mail.toString());
        MailBean mail = new MailBean();
        mail.setSubject("创奇科技");
        mail.setText("您注册了XX系统账号，欢迎使用！");

//        MessageProperties properties = message.getMessageProperties();
//        long tag = properties.getDeliveryTag();

        mailUtil.sendMail(mail);
//        if (success) {
//            channel.basicAck(tag, false);// 消费确认
//        } else {
//            channel.basicNack(tag, false, true);
//        }




        /*String to = mail.getTo();// 目标邮箱
        String title = mail.getSubject();// 邮件标题
        String content = mail.getText();// 邮件正文

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("ihopyou@qq.com");
        message.setTo(to);
        message.setSubject(title);
        message.setText(content);

        try {
            mailSender.send(message);
            logger.info("邮件发送成功");
            return true;
        } catch (MailException e) {
            logger.error("邮件发送失败, to: {}, title: {}", to, title, e);
            return false;
        }*/


//        MailBean mailBean = new MailBean();
//        mailBean.setSubject("学科竞赛系统注册");
//        mailBean.setText(msg);
//        //发送邮件
//        mailUtil.sendMail(mailBean);
    }


}
